import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);
        let that = this;
        that.prov = {
            subs: []
        };
        new Vue({
            el: "#prov_vue",
            data: {prov: that.prov},
            methods: {
                clickCity: function (city) {
                    //如果已经勾选
                    if (city.hasChoosed) {
                        city.hasChoosed = false;
                        //如果没有勾选
                    } else {
                        city.hasChoosed = true;
                    }
                },
                updateArea: function () {
                    let choosedArea = [];
                    let citys = this.prov.subs;
                    // 取出已经选择了的城市id
                    for (let i = 0, len = citys.length; i < len; i++) {
                        if (citys[i].hasChoosed == true) {
                            choosedArea.push(citys[i].adcode);
                        }
                    }
                    let data = {};
                    //如果都不选择，
                    if (choosedArea.length == 0) {
                        data.areas = [this.prov.adcode];
                        data.type = 2;
                    } else {
                        data.areas = choosedArea;
                        data.type = 3;
                    }
                    api.updateServiceArea(data, {
                        succ: function (json) {
                            //更新全局数据
                            globalData.pack.area_id = json.pack.area_id;
                            //更新省份列表模块的当前省信息
                            if (choosedArea.length == 0) {
                                that.prov.containType = 0;
                                that.prov.containInfo = "未关注";
                            } else {
                                that.prov.containType = 1;
                                that.prov.containInfo = "关注部分城市";
                            }

                            if (choosedArea.length == citys.length) {
                                that.prov.containType = 2;
                                that.prov.containInfo = "关注全省";
                            }
                            funs.getModuleFuns('modules/user/ServiceArea.js', function (mod) {
                                mod.setProvData(that.prov);
                            });
                            //修改业务范围后状态更改
                            globalData.user.has_set_business = true;
                            that._evt_goback();
                        },
                        fail: function () {
                            funs.tip('提交失败', '请检查网络状态并刷新页面重试', 'fail');
                            setTimeout(that._evt_goback, 2000);
                        }
                    });
                }
            }
        });
    }

    initTpl(prov) {
        let that = this;
        that.prov.adcode = prov.adcode;
        that.prov.name = prov.name;
        that.prov.provCity = prov.provCity;
        that.prov.containType = prov.containType;
        that.prov.containInfo = prov.containInfo;
        that.prov.subs.length = 0;
        that.prov.subs.push(...prov.subs);
    }

    _evt_chooseAllProv(obj) {
        let that = this;
        let option = $(obj).find('.pull-right');
        if (option.hasClass('option')) {
            option.removeClass('option').addClass('icon-checkmark');
            for (let i = 0, len = that.prov.subs.length; i < len; i++) {
                that.prov.subs[i].hasChoosed = true;
            }
            that.prov.containType = 2;
            that.prov.containInfo = "关注全省";
        } else {
            option.removeClass('icon-checkmark').addClass('option');
            for (let i = 0, len = that.prov.subs.length; i < len; i++) {
                that.prov.subs[i].hasChoosed = false;
            }
            that.prov.containType = 0;
            that.prov.containInfo = "未关注";
        }
    }


}